<template>
	<view class="login-container">
		<view class="wechatapp">
			<view class="header"><open-data class="" type="userAvatarUrl"></open-data></view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			<!-- #ifdef MP-WEIXIN -->
			<!-- <view>
				<button class="btn-normal" @click="getUserProfile"></button>
			</view> -->
			<button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">授权登录</button>
			<!-- #endif -->
		</view>
		<view class="no-login-btn"><button class="btn-normal" @click="onNotLogin">暂不登录</button></view>
	</view>
</template>

<script>
// import WXBizDataCrypt from "@/common/WXBizDataCrypt.js";
export default {
	data() {
		return {
			background: '',
			listData: [],
			userinfo: [],
			session_key: '',
			token: '',
			option: {}
		};
	},
	onShow() {
		//wx.login(); //重新登录
	},
	onLoad(option) {
		this.option = option;
		console.log(option, '参数');
		this.session_key = uni.getStorageSync('session_key');
		console.log(this.session_key, 'session_key');
		// uni.removeStorageSync('session_key');
		this.userinfo = option.userinfo;
	},
	methods: {
		/*改变发送验证码按钮文本*/
		changeMsg() {
			if (this.second > 0) {
				this.send_btn_txt = this.second + '秒';
				this.second--;
				setTimeout(this.changeMsg, 1000);
			} else {
				this.send_btn_txt = '获取验证码';
				this.second = 60;
				this.is_send = false;
			}
		},
		// getUserProfile() {
		// 	let self = this;
		// 	uni.getUserProfile({
		// 		desc: 'weixin',
		// 		success: function(infoRes) {
		// 			self.userinfo = infoRes.userInfo
		// 			console.log(infoRes,"getUserProfile数据")
		// 			uni.login({
		// 				provider: 'weixin',
		// 				success: function(loginRes) {
		// 					console.log(loginRes,"login数据")
		// 					self._post('/api/login/index', {
		// 							js_code: loginRes.code,
		// 							avatar: self.userinfo.avatarUrl,
		// 							nickName: self.userinfo.nickName
		// 						}, function(res) {
		// 							console.log(res, "参数22222222")
		// 							self.session_key = res.data.session_key
		// 							uni.setStorageSync('token', res.data.token)
		// 							uni.setStorageSync('user_id', res.data.user_id)
		// 							uni.setStorageSync('userinfo', res.data)
		// 						},
		// 						function(err) {
		// 							console.log('err', err);
		// 						})
		// 				}
		// 			});
		// 		}
		// 	});
		// },
		onNotLogin: function() {
			let pages = getCurrentPages();
			uni.reLaunch({
				url: pages[0].$page.fullPath
			});
			uni.clearStorage();
		},
		onGetPhoneNumber(e) {
			let self = this;
			if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
				//用户决绝授权
				//拒绝授权后弹出一些提示
				uni.clearStorage();
			} else {
				//允许授权
				self._post(
					'/api/login/sessionKey',
					{
						session_key: self.session_key,
						encrypted_data: e.detail.encryptedData,
						iv: e.detail.iv
					},
					function(res) {
						self.userinfo = JSON.parse(self.userinfo);
						self.userinfo.mobile = res.data.mobile;
						uni.setStorageSync('userinfo', self.userinfo);
						uni.setStorageSync('token', self.option.token);
						
						let pages = getCurrentPages();
						uni.reLaunch({
							url: pages[0].$page.fullPath
						});
					},
					function(err) {
						console.log('err', err);
					}
				);
			}
		}
	}
};
</script>

<style>
.login-container {
	padding: 130rpx;
}

.wechatapp {
	padding: 80rpx 0 48rpx;
	border-bottom: 1rpx solid #e3e3e3;
	margin-bottom: 72rpx;
	text-align: center;
}

.wechatapp .header {
	width: 190rpx;
	height: 190rpx;
	border: 2px solid #fff;
	margin: 0rpx auto 0;
	border-radius: 50%;
	overflow: hidden;
	box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
}

.auth-title {
	color: #585858;
	font-size: 34rpx;
	margin-bottom: 40rpx;
}

.auth-subtitle {
	color: #888;
	margin-bottom: 88rpx;
	font-size: 28rpx;
}

.login-btn {
	padding: 0 20rpx;
}

.login-btn button {
	height: 88rpx;
	line-height: 88rpx;
	background: #04be01;
	color: #fff;
	font-size: 30rpx;
	border-radius: 999rpx;
	text-align: center;
}

.no-login-btn {
	margin-top: 20rpx;
	padding: 0 20rpx;
}

.no-login-btn button {
	height: 88rpx;
	line-height: 88rpx;
	background: #dfdfdf;
	color: #fff;
	font-size: 30rpx;
	border-radius: 999rpx;
	text-align: center;
}
</style>
